package com.jeomo.mem.starter.sysoption;

import com.jeomo.mem.starter.enums.YearTypeEnum;
import com.jeomo.sys.provider.sysoption.BaseSysOption;
import com.jeomo.sys.provider.sysoption.OptionField;
import com.jeomo.sys.provider.sysoption.OptionType;
import com.jeomo.sys.provider.sysoption.SystemOption;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 积分有效期_系统选项
 *
 * @author:Jackpot
 * @create 2020-12-08 12:07
 * @description:
 */
@SystemOption(code = "pointsValiditySysOption", name = "积分有效期配置")
public class PointsValiditySysOption extends BaseSysOption{

    @OptionField(type = OptionType.INT_TYPE)
    /** 积分兑换截止日(天)*/
    private Integer convertDeadline;

    @OptionField(type = OptionType.STRING_TYPE)
    /** 积分有效期(天)*/
    private String option;;
    @OptionField(type = OptionType.INT_TYPE)
    /** 积分有效期(天)*/
    private Integer expireDays;
    @OptionField(type = OptionType.INT_TYPE)
    /** 有效期:年份类型*/
    private Integer yearType;
    @OptionField(type = OptionType.DATE_TYPE)
    /** 有效期:失效时间(只取月和日)*/
    private LocalDate expireDate;


    public String getOption() {
        return option;
    }

    public void setOption(String option) {
        this.option = option;
    }

    public Integer getExpireDays() {
        return expireDays;
    }

    public void setExpireDays(Integer expireDays) {
        this.expireDays = expireDays;
    }

    public Integer getYearType() {
        return yearType;
    }

    public void setYearType(Integer yearType) {
        this.yearType = yearType;
    }

    public LocalDate getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(LocalDate expireDate) {
        this.expireDate = expireDate;
    }

    public Integer getConvertDeadline() {
        return convertDeadline;
    }

    public void setConvertDeadline(Integer convertDeadline) {
        this.convertDeadline = convertDeadline;
    }

    /**
     * 获取全局的积分过期时间<br/>
     * 自动调用 load(),不需要重复调用
     * @param date 积分获取时间
     * @return
     */
    public LocalDate getValidityDate(LocalDate date){
        return getValidityDate(true,date);
    }

    /**
     *
     * @param autoLoad  自动加载数据
     * @param date 积分获取时间
     * @return
     */
    public LocalDate getValidityDate(boolean autoLoad,LocalDate date){
        if (autoLoad) load();
        LocalDate acquireDate = date;
        Integer expireDays = getExpireDays();
        if (expireDays > 0){
            return acquireDate.plusDays(expireDays);
        }else if(expireDays < 0){
            return acquireDate;
        }
        //expireDays 为0时
        LocalDate expireDate = getExpireDate();
        int currYear = acquireDate.getYear();
        if (YearTypeEnum.THIS_YEAR.getCode() == getYearType()){
            expireDate.withYear(currYear);
        }else if(YearTypeEnum.SECOND_YEAR.getCode() == getYearType()){
            expireDate.withYear(currYear+1);
        }else{
            expireDate = acquireDate;
        }
        return expireDate;
    }
}
